/**
 * Created by Administrator on 2019/2/15.
 */
$(function () {
    layui.use('form', function () {
        var form = layui.form;
        if ($("#layui-form").length > 0) {
            form.on('submit(formSubmit)', function (data) {
                // console.log(data.field)
                $.ajax({
                    url: $(data.form).attr('action'),
                    type: 'POST',
                    dataType: 'json',
                    data: data.field,
                    success: function (res) {
                        // console.log(res)
                        if (res != '') {
                            if (res.hasOwnProperty('code') && res.code == 200) {
                                layer.msg(res.message, {icon: 1, time: 2000}, function () {
                                    if (res.redirect != 'parent') {
                                        window.location.href = res.redirect;
                                    } else {
                                        window.location.href = document.referrer;
                                    }
                                });
                                return false;
                            } else {
                                layer.msg(res.message, {icon: 2});
                            }
                        }
                    }
                });
                return false;
            });
        }
    });
});
